package main

import (
    "../medusa"
)

type GlobalTask struct {
    world_server *World
    gate_server  *GateService
}

var global *GlobalTask = nil

func NewGlobal(gate *GateService) {
    if global == nil {
        global = new(GlobalTask)
        global.world_server = nil
        global.gate_server = gate
    }
}

func Global() *GlobalTask {
    return global
}

func (self *GlobalTask) SetWorld(world *World) {
    self.world_server = world
}

func (self *GlobalTask) World() *World {
    return self.world_server
}

func (self *GlobalTask) GateServer() *GateService {
    return self.gate_server
}

func (self *GlobalTask) SmartSend(pack *medusa.Packet) {
    if self.gate_server != nil {
        if pack.IsSpacePacket() {
            self.gate_server.SpaceBroadcast(pack)
        } else {
            self.gate_server.SendPacket((int)(pack.GetSessionUUID()), pack)
        }
    }
}

func (self *GlobalTask) SendToClient(client_uuid int32, pack *medusa.Packet) {
    if self.gate_server != nil {
        self.gate_server.SendPacket((int)(client_uuid), pack)
    }
}

func (self *GlobalTask) ClientBroadcast(pack *medusa.Packet) {
    if self.gate_server != nil {
        self.gate_server.Broadcast(pack, (int)(pack.GetExceptUUID()))
    }
}

func (self *GlobalTask) SendToWorld(pack *medusa.Packet) {
    if self.world_server != nil {
        self.world_server.SendPacket(pack)
    }
}
